﻿using GameLibrary.Model;
using GameLibrary.Model.Core;
using GameLibrary.Model.Enums;
using G.Runtime;

namespace GameDesigner.Source.Test
{
    public class Conqure
    {
        public static void Build(GameDef game)
        {
            game.Name = "Conqure";
            {
                GameElementDef element = GameDefFactory.Factory.CreateElement(
                    game, new GameLocation(0, 0));
                element.Player = "Player1";
                game.GameElementDefs.Add(element);
                AddGold(element);
            }
            {
                GameElementDef element = GameDefFactory.Factory.CreateElement(
                    game, new GameLocation(9, 0));
                element.Player = "Player2";
                game.GameElementDefs.Add(element);
                AddGold(element);
            }
            AddCoin(game, new GameLocation(4, 0));
            AddCoin(game, new GameLocation(5, 0));
            
            AddFarm(game, new GameLocation(0, 1));
            AddFarm(game, new GameLocation(9, 1));
            {
                game.GameElementDefs.GameScriptDef.Text = @"public class GameElementsDefs_Script
{
    private G.Runtime.GameElements _Subject;

    public GameElementsDefs_Script(G.Runtime.GameElements subject)
    {
        _Subject = subject;
        _Subject.Triggers.ConsumeEvent += OnConsumeEvent;
        _Subject.Triggers.OwnEvent += OnOwnEvent;
    }

    void OnConsumeEvent(G.Runtime.GameElement consumer, G.Runtime.GameElement consumee)
    {
        consumer.GameValues.Add(consumee.GameValues);
        consumer.Location = consumee.Location;
    }
    
    void OnOwnEvent(G.Runtime.GameElement attacher, G.Runtime.GameElement defender)
    {
        defender.Player = attacher.Player;
        defender.AssignTaskActions();
    }
    
    void OnFightEvent(G.Runtime.GameElement attacher, G.Runtime.GameElement defender)
    {
    }
}
";
                
                game.GamePlayerDefs.GameScriptDef.Text = @"public class GamePlayerDefs_Script
{
    private G.Runtime.GamePlayers _Subject;

    public GamePlayerDefs_Script(G.Runtime.GamePlayers subject)
    {
        _Subject = subject;
        _Subject.Triggers.EndOfTurnEvent += OnEndOfTurnEvent;
    }

    void OnEndOfTurnEvent(G.Runtime.GameEvent turnEvent)
    {
    	 turnEvent.FireInvoke();
    }

}
";
            }
        }

        private static void AddGold(GameElementDef element)
        {
            GameValueDef gold = new GameValueDef();
            gold.Name = "Gold";
            gold.TypeCode = System.TypeCode.Int32;
            gold.Value = "0";
            element.GameValueDefs.Add(gold);
        }
        
        private static void AddCoin(GameDef game, GameLocation location)
        {
            GameElementDef element = GameDefFactory.Factory.CreateElement(
                game, location);
            element.Icon = "Coin";
            element.Player = "Nothing";
            element.CollisionStratagy = CollisionStratagyEnum.CONSUME;
            GameValueDef v = new GameValueDef();
            v.Value = "100";
            element.GameValueDefs.Add(v);
            game.GameElementDefs.Add(element);
        }
        
        private static void AddFarm(GameDef game, GameLocation location)
        {
            GameElementDef element = GameDefFactory.Factory.CreateElement(
                game, location);
            element.Icon = "Farm";
            element.Player = "Nothing";
            element.CollisionStratagy = CollisionStratagyEnum.OWN;
            GameValueDef v = new GameValueDef();
            v.Value = "0";
            element.GameValueDefs.Add(v);
            game.GameElementDefs.Add(element);
        }
        
    }
}
